Micron Document




Project Valhalla (Java language)
part 3/6 · 8.3 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Value classes still support null, since they are reference types. The Java Virtual Machine is expected to be able to take advantage of the additional constraints of value classes to eliminate heap allocation of value types in the vast majority of cases. However, storing instances of value classes into a field or upcasting them to an interface will still require an allocation.

Existing types in the Java API such as java.util.Optional are known as value-based classes, and are candidates for being made into value classes in a future JDK release.

Primitive classes

Primitive classes are subject to all the constraints of value classes, but are not reference types. This means they give up the ability to support null. Instead, their default values are the zero value for each of the component types (0 for numerical types, false for booleans, null for reference types, and the zero value for nested primitive classes).

All primitive classes are stored "inline", that is, without requiring a heap allocation. Arrays of primitive classes will not require a pointer indirection from the array to the heap. Where needed, conversions will be inserted to "box" the primitive class into a value class version of itself and vice versa.

Classes for the basic primitives

This JEP is meant to express the classical primitive types of the Java Virtual Machine (byte, char, short, int, long, boolean, float, double) as primitive classes.

Traditionally, the eight primitive types are treated separately from all other types. Providing primitive class declarations from them removes much of this special-casing, leading to a more elegant and easy to understand type system.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────